package com.cyandpos.pad.activity;

import android.app.ActivityManager;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.WindowManager;

import com.cyandpos.pad.R;
import com.changyi.device.APApplication;
import com.cyandpos.pad.presentation.AdPresentationUtil;
import com.cyandpos.pad.utils.AppManager;
import com.cyandpos.pad.utils.log.LogUtils;

import butterknife.ButterKnife;


/**
 * Created by lc on 2020/2/7.
 * 所有Activity的基类
 */

public abstract class BaseActivity extends AppCompatActivity {
    private boolean isRightTitle;
    public boolean isRightIcon;
    private String rightTitle;
    private int img;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        beforeSetContentView();
        super.onCreate(savedInstanceState);
        LogUtils.d("onCreate()方法");
        if (getLayoutId() != 0){
            setContentView(getLayoutId());
        }
        ButterKnife.bind(this);
        ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
        ActivityManager.RunningTaskInfo info = manager.getRunningTasks(1).get(0);
        String className = info.topActivity.getClassName();//完整类名 com.cyandpos.andpos.activity.MainSaleActivity
        int index = className.lastIndexOf(".");
        String currentClassName = className.substring(index+1);
        AppManager.geInstance().addActivity(currentClassName,this);
        APApplication.getInstance().addActivity(this);
        new AdPresentationUtil().initDisplayData(this);
        initData();
    }

    /**
     * super前加载
     */
    protected  void beforeSetContentView(){
       // getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);//防止截屏
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);  //防止软键盘把布局顶上去
    }

    /**
     * 初始化数据，抽象
     */
    protected abstract void initData();
    /**
     * 获取layout Id
     * @return
     */
    protected abstract int getLayoutId();

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.title_menu, menu);
        return true;
    }

    /**
     * title的设置
     * @param toolbar
     * @param title
     */
    protected void SetToolBar(Toolbar toolbar, String title) {
        toolbar.setTitle(title);
        setSupportActionBar(toolbar);
    }
    /**
     * 设置标题右边的文字
     * @param titleName
     */
    protected void SetToolBarRightTxt(String titleName) {
        this.isRightTitle = true;
        this.rightTitle = titleName;
        invalidateOptionsMenu();
    }
    /**
     * 设置标题右边的Icon
     * @param img
     */
    protected void SetToolBarRightTxt(int img) {
        this.isRightIcon = true;
        this.img = img;
        invalidateOptionsMenu();
    }
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        if (isRightTitle) {
            menu.findItem(R.id.menu_right).setVisible(true);
            menu.findItem(R.id.menu_right).setTitle(rightTitle);
        }else if(isRightIcon){
            menu.findItem(R.id.icon_right).setVisible(true);
            menu.findItem(R.id.icon_right).setIcon(getResources().getDrawable(img));
        }
        return super.onPrepareOptionsMenu(menu);
    }


    /**
     * Activity跳转
     * @param clazz
     * @param bundle
     */
    public void go2Activity(Class clazz, Bundle bundle){
        Intent intent  = new Intent(this,clazz);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }
    /**
     * 有返回的跳转
     * @param clazz
     * @param bundle
     * @param requestCode
     */
    public void go2ActivityForResult(Class clazz, Bundle bundle,int requestCode){
        Intent intent  = new Intent(this,clazz);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivityForResult(intent,requestCode);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_MENU || keyCode == KeyEvent.KEYCODE_HOME){
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 屏蔽home键_手机测试可以先屏蔽掉
     */
//    @Override
//    public void onAttachedToWindow() {
//        this.getWindow().setType(
//                WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
//        super.onAttachedToWindow();
//    }
}
